<?php
namespace Core\CoreContracts;

use Core;

require_once (CONTRACTS_DIR . '/stateeventsubscription.enum.php');

/*
* Event subscription model definition. An user can presubsribe to an event then the default number of places reserved is 1. User can modify this value on his account.
*
* @method Create($event, $user)
* @method Plan()
* @method Pay()
* @method Cancel()
* @method Valid()
*/
class EventSubscription {

	/**
    * @property integer $Id The event subscription unique identifier
    */
	public $Id;
	
	/**
    * @property integer $State The state event subscription
    */
	public $State;
	
	/**
    * @property integer $NumberOfPlace The user can reserve several number of place
    */
	public $NumberOfPlaces;

	/**
    * @property Event $Event The associated event
    */
	public $Event;
	
	/**
    * @property User $User The associated user to event
    */
	public $User;
	
	/**
    * @property boolean $IsPartner The associated user is a partner
    */
	public $IsPartner;
	
	/**
    * @property datetime $ModificationDate The event subscription modification date
    */
	public $ModificationDate;
	
	/**
    * @property datetime $CreationDate The event subscription creation date
    */
	public $CreationDate;
	
	/**
	* The default constructor
	*/
	public function __construct()  {  }
	
	/*
	* Build a new event subscription object
	*
	* @param Event $event The event
	* @param User $user The user
	*/
	public static function Create($event, $user)
	{
		$eventSubscription = new EventSubscription();
		$eventSubscription->NumberOfPlaces = 1;
		$eventSubscription->Event = $event;
		$eventSubscription->User = $user;
		$eventSubscription->Plan();
		$eventSubscription->CreationDate = date("Y-m-d H:i:s");
		return $eventSubscription ;
	}
	
	/*
	*  Change state event to planned
	*/
	public function Plan()
	{
		//Check user state
		if($this->User->State == Core\CoreContracts\StateUser::Banned)
		{
			throw new \Exception("The user is banned by the system.");
		}
		
		//Check number of places
		if($this->Event->NumberOfPlaces == 0)
		{
			throw new \Exception("No more place.");
		}
		
		//Check event state
		if($this->Event->State == Core\CoreContracts\StateEvent::CloseSubscriptions)
		{
			throw new \Exception("The event subscriptions are closed.");
		}
		
		if($this->Event->State == Core\CoreContracts\StateEvent::Running)
		{
			throw new \Exception("The event is running, you can't subscribe.");
		}
		
		if($this->Event->State == Core\CoreContracts\StateEvent::Done)
		{
			throw new \Exception("The event was done, you can't subscribe.");
		}
		
		if($this->Event->State == Core\CoreContracts\StateEvent::Cancelled)
		{
			throw new \Exception("The event was cancelled, you can't subscribe.");
		}
		
		//Check event subscription state
		if($this->State ==Core\CoreContracts\ StateEventSubscription::Paid)
		{
			throw new \Exception("The subscription is paid, you have to wait that has been valid.");
		}
		
		if($this->State == Core\CoreContracts\StateEventSubscription::Valid)
		{
			throw new \Exception("The subscription is valid, you can't replanned this event subscription.");
		}

		$this->State = Core\CoreContracts\StateEventSubscription::Planned;
		$this->ModificationDate = date("Y-m-d H:i:s");
	}
	
	/*
	*  Change state event to pay
	*/
	public function Pay()
	{
		//Check user state
		if($this->User->State == Core\CoreContracts\StateUser::Banned)
		{
			throw new \Exception("The user is banned by the system.");
		}
		
		//Check event state
		if($this->Event->State == Core\CoreContracts\StateEvent::Cancelled)
		{
			throw new \Exception("The event was cancelled, you can't paid.");
		}
	
		//Check event subscription state
		if($this->State == Core\CoreContracts\StateEventSubscription::Paid)
		{
			throw new \Exception("The subscription is already paid.");
		}
		
		if($this->State == Core\CoreContracts\StateEventSubscription::Cancelled)
		{
			throw new \Exception("The subscription was cancelled, you can't pre-paid this event.");
		}
		
		$this->State = Core\CoreContracts\StateEventSubscription::Paid;
		$this->ModificationDate = date("Y-m-d H:i:s");
	}
	
	/*
	*  Change state event to cancelled
	*/
	public function Cancel()
	{
		//Check event subscription state
		if($this->State == Core\CoreContracts\StateEventSubscription::Cancelled)
		{
			throw new \Exception("The subscription is already cancelled.");
		}
		
		//@todo If this event was pre-paid process the money back
		
		$this->State = Core\CoreContracts\StateEventSubscription::Cancelled;
		$this->ModificationDate = date("Y-m-d H:i:s");
	}
	
	/*
	*  Change state event to valid
	*/
	public function Valid()
	{
		//Check user state
		if($this->User->State == Core\CoreContracts\StateUser::Banned)
		{
			throw new \Exception("The user is banned by the system.");
		}
		
		//Check event state
		if($this->Event->State == Core\CoreContracts\StateEvent::Done)
		{
			throw new \Exception("The event was done, you can't add a new subscription.");
		}
		
		if($this->Event->State == Core\CoreContracts\StateEvent::Cancelled)
		{
			throw new \Exception("The event was cancelled, you can't add a new subscription.");
		}
	
		//Check event subscription state
		if($this->State == Core\CoreContracts\StateEventSubscription::Valid)
		{
			throw new \Exception("The subscription is already valid.");
		}
	
		if($this->State == Core\CoreContracts\StateEventSubscription::Cancelled)
		{
			throw new \Exception("The subscription has been cancelled.");
		}

		$this->State = Core\CoreContracts\StateEventSubscription::Valid;
		$this->ModificationDate = date("Y-m-d H:i:s");
	}
}
?> 